package cn.sc.summer.oss.controller;

import cn.sc.summer.constant.model.Result;
import cn.sc.summer.domain.service.MinioService;
import cn.sc.summer.feign.client.OssFeignClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * 类名：oss测试控制类
 *
 * @author a-xin
 * @date 2024-01-12 09:29:59
 */
@Slf4j
@RestController
@RequestMapping("/oss")
@Api(tags = "Oss-server测试")
public class OssController implements OssFeignClient {

    @Resource
    private MinioService minioService;

    /**
     * 上传文件
     *
     * @param uploadFile 文件流
     * @return 文件地址
     */
    @Override
    @ApiOperation(value = "上传文件")
    @PostMapping(value = "/uploadFile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result<String> uploadFile(@RequestPart(value = "uploadFile") MultipartFile uploadFile) {
        return Result.success(minioService.uploadFile(uploadFile));
    }

    /**
     * 下载文件
     *
     * @param fileId   文件地址
     * @param response 返回数据信息
     * @return 文件地址
     */
    @Override
    @ApiOperation(value = "下载文件")
    @GetMapping(value = "/downloadFile")
    public Result<String> downloadFile(@RequestParam(value = "fileId") String fileId, HttpServletResponse response) {
        minioService.downloadFile(fileId, response);
        return Result.success();
    }

}
